package com.example.prom.ok.y2022.controller.kyc3.loqate;

import com.example.prom.ok.y2022.dto.loqate.CaptureFindReqDTO;
import com.example.prom.utils.HttpClientSingleton;
import com.example.prom.utils.HttpNewUtils;
import com.example.prom.utils.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("loqate")
@Controller
public class loqateTestController {

    @PostMapping("test")
    public void test(@RequestBody CaptureFindReqDTO captureFindReqDTO){
        try {
            String requestUrl = "https://api.addressy.com/Capture/Interactive/Find/v1.1/json3.ws";

            HttpClient httpClient = HttpClientSingleton.getLongCustomHttpClient();

            Map<String, String> queryMap = new HashMap<>();
            queryMap.put("key", "ZK21-TE16-DC95-HR81");
            queryMap.put("Text", captureFindReqDTO.getText());
//            queryMap.put("IsMiddleware", true);
            queryMap.put("Countries", captureFindReqDTO.getCountries());
            queryMap.put("Limit", captureFindReqDTO.getLimit().toString());
            queryMap.put("Language", captureFindReqDTO.getLanguage());
            HttpResponse httpResponse = HttpNewUtils.doPostByForm(requestUrl, null, httpClient, null, queryMap, null);
            String result = IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("test2")
    public void test2(@RequestParam String id){
        try {
            String requestUrl = "https://api.addressy.com/Capture/Interactive/Retrieve/v1.1/json3.ws";

            HttpClient httpClient = HttpClientSingleton.getLongCustomHttpClient();

            Map<String, String> queryMap = new HashMap<>();
            queryMap.put("key", "ZK21-TE16-DC95-HR81");
            queryMap.put("id", id);
            HttpResponse httpResponse = HttpNewUtils.doPostByForm(requestUrl, null, httpClient, null, queryMap, null);
            String result = IOUtils.toString(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
